package cc.hotice.generator.plugin;

import cc.hotice.generator.element.SelectAndLockByPrimaryKeyElementGenerator;
import cc.hotice.generator.element.SelectListWithConditionElementGenerator;
import cc.hotice.generator.mapper.SelectAndLockByPrimaryKeyMethodGenerator;
import cc.hotice.generator.mapper.SelectListWithConditionMethodGenerator;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.Interface;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.java.TopLevelClass;
import org.mybatis.generator.api.dom.xml.Document;
import org.mybatis.generator.api.dom.xml.XmlElement;

import java.util.List;

public class MyMapperPlugin extends PluginAdapter {

    public boolean validate(List<String> list) {
        return true;
    }

    /**
     * 不生成
     *
     * @param method
     * @param interfaze
     * @param introspectedTable
     * @return
     */
    @Override
    public boolean clientInsertMethodGenerated(Method method, Interface interfaze, IntrospectedTable introspectedTable) {
        return false;
    }

    /**
     * 不生成
     *
     * @param element
     * @param introspectedTable
     * @return
     */
    @Override
    public boolean sqlMapInsertElementGenerated(XmlElement element, IntrospectedTable introspectedTable) {
        return false;
    }

    /**
     * 不生成
     *
     * @param method
     * @param interfaze
     * @param introspectedTable
     * @return
     */
    @Override
    public boolean clientUpdateByPrimaryKeyWithoutBLOBsMethodGenerated(Method method, Interface interfaze, IntrospectedTable introspectedTable) {
        return false;
    }

    /**
     * 不生成
     *
     * @param element
     * @param introspectedTable
     * @return
     */
    @Override
    public boolean sqlMapUpdateByPrimaryKeyWithoutBLOBsElementGenerated(XmlElement element, IntrospectedTable introspectedTable) {
        return false;
    }

    @Override
    public boolean clientGenerated(Interface interfaze, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {

        addSelectAndLockByPrimaryKeyMethod(interfaze, introspectedTable);
        addSelectListWithConditionMethod(interfaze, introspectedTable);

        return super.clientGenerated(interfaze, topLevelClass, introspectedTable);
    }

    @Override
    public boolean sqlMapDocumentGenerated(Document document, IntrospectedTable introspectedTable) {

        addSelectAndLockByPrimaryKeyElement(document, introspectedTable);
        addSelectListWithConditionElement(document, introspectedTable);

        return super.sqlMapDocumentGenerated(document, introspectedTable);
    }

    private void addSelectAndLockByPrimaryKeyMethod(Interface interfaze, IntrospectedTable introspectedTable) {
        SelectAndLockByPrimaryKeyMethodGenerator methodGenerator = new SelectAndLockByPrimaryKeyMethodGenerator(false);
        methodGenerator.setContext(context);
        methodGenerator.setIntrospectedTable(introspectedTable);
        methodGenerator.addInterfaceElements(interfaze);
    }

    private void addSelectListWithConditionMethod(Interface interfaze, IntrospectedTable introspectedTable) {
        SelectListWithConditionMethodGenerator methodGenerator = new SelectListWithConditionMethodGenerator();
        methodGenerator.setContext(context);
        methodGenerator.setIntrospectedTable(introspectedTable);
        methodGenerator.addInterfaceElements(interfaze);
    }

    private void addSelectListWithConditionElement(Document document, IntrospectedTable introspectedTable) {
        SelectListWithConditionElementGenerator elementGenerator = new SelectListWithConditionElementGenerator();
        elementGenerator.setContext(context);
        elementGenerator.setIntrospectedTable(introspectedTable);
        elementGenerator.addElements(document.getRootElement());
    }

    private void addSelectAndLockByPrimaryKeyElement(Document document, IntrospectedTable introspectedTable) {
        SelectAndLockByPrimaryKeyElementGenerator elementGenerator = new SelectAndLockByPrimaryKeyElementGenerator();
        elementGenerator.setContext(context);
        elementGenerator.setIntrospectedTable(introspectedTable);
        elementGenerator.addElements(document.getRootElement());
    }
}
